package tester.neural.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

public class MouseComponent extends JComponent implements MouseMotionListener {
	private static final long serialVersionUID = 1L;
	private static int SIZE_X = 100;
	private static int SIZE_Y = 100;
	
	private static BufferedImage image;
	
	public MouseComponent() {
		getImage();
		
		addMouseMotionListener(this);
	}
	
	public static BufferedImage getImage() {
		if (image == null)
			clearImage();
		
		return image;
	}
	
	public static void clearImage() {
		image = new BufferedImage(SIZE_X, SIZE_Y, BufferedImage.TYPE_INT_ARGB_PRE);
		
		Graphics g = image.getGraphics();
		
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, image.getWidth(), image.getHeight());
		
		g.setColor(Color.BLACK);
		g.drawRect(1, 1, 95, 95);
	}
	
	@Override
	public void mouseDragged(MouseEvent event) {
		Graphics g = MouseComponent.getImage().getGraphics();
		
		g.setColor(Color.BLUE);
		Point p = event.getPoint();
		
		if (p.x > 1 && p.y > 0 && p.x + 3 < 94 && p.y + 3 < 94) 
			g.fillOval(p.x, p.y, 7, 7);
		
		g.dispose();
		repaint();
	}

	@Override
	public void mouseMoved(MouseEvent event) {
		
	}
	
	@Override
	public void paintComponent(Graphics g) {
		g.drawImage(MouseComponent.getImage(), 0, 0, null);
	}
}
